﻿@model ShipmentListModel
@using Telerik.Web.Mvc.UI
@{
    var gridPageSize = EngineContext.Current.Resolve<SmartStore.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    ViewBag.Title = T("Admin.Orders.Shipments.List").Text;
}

@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <i class="fa fa-truck"></i>
            @T("Admin.Orders.Shipments.List")
        </div>
        <div class="options">
            @if (Model.DisplayPdfPackagingSlip)
            {
                <a href="@Url.Action("PdfPackagingSlips", new { all = true })" class="btn btn-secondary">
					<i class="fa fa-print"></i>
					<span>@T("Admin.Orders.Shipments.PrintPackagingSlip.All")</span>
				</a>
                <button type="submit" id="exportpackagingslips-selected" class="btn btn-secondary">
					<i class="fa fa-print"></i>
					<span>@T("Admin.Orders.Shipments.PrintPackagingSlip.Selected")</span>
				</button>
            }
        </div>
    </div>

	<div class="row mt-3 grid-filter">
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.StartDate)
			@Html.EditorFor(model => model.StartDate)
		</div>
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.EndDate)
			@Html.EditorFor(model => model.EndDate)
		</div>

		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.TrackingNumber)
			@Html.EditorFor(model => model.TrackingNumber)
		</div>

		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabel(string.Empty, string.Empty)
			<button type="submit" id="btnSearch" value="btnSearch" class="btn btn-secondary btn-block">
				<span>@T("Admin.Common.Search")</span>
			</button>
		</div>
	</div>


<div>
	@(Html.Telerik().Grid<ShipmentModel>()
		.Name("shipments-grid")
		.ClientEvents(events => events
			.OnDataBinding("onDataBinding")
			.OnDataBound("onDataBound"))
		.Columns(columns =>
		{
			columns.Bound(x => x.Id)
			.ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= Id #>' class='checkboxGroups'/>")
			.Title("<input id='mastercheckbox' type='checkbox'/>")
			.Width(50)
			.HtmlAttributes(new { style = "text-align:center" })
			.HeaderHtmlAttributes(new { style = "text-align:center" });
			columns.Bound(x => x.Id);
			columns.Bound(x => x.OrderId)
				.Width(80)
				.Centered()
				.Template(x => Html.ActionLink(T("Admin.Common.View").Text, "Edit", "Order", new { id = x.OrderId }, new { }))
				.ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Order/Edit/") + "<#= OrderId #>\"><#= OrderId #></a>");
			columns.Bound(x => x.TrackingNumber);
			columns.Bound(x => x.TotalWeight);
			columns.Bound(x => x.ShippedDate);
			columns.Bound(x => x.DeliveryDate);
			columns.Bound(x => x.Id)
				.Template(x => Html.ActionLink(T("Admin.Common.View").Text, "ShipmentDetails", "Order", new { id = x.Id }, new { @class = "t-button" }))
				.ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Order/ShipmentDetails/") + "<#= Id #>\" class=\"t-button\">" + T("Admin.Common.View").Text + "</a>")
				.Width(50)
				.Centered()
				.Title(String.Empty);
		})
		.Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
		.DataBinding(dataBinding => dataBinding.Ajax().Select("ShipmentListSelect", "Order"))
		.PreserveGridState()
		.EnableCustomBinding(true))
</div>

    
    <script type="text/javascript">

        var selectedIds = [];

        $(document).ready(function () {

            //search button
            $('#btnSearch').click(function () {
                //search
                var grid = $('#shipments-grid').data('tGrid');
                grid.currentPage = 1; //new search. Set page size to 1
                grid.ajaxRequest();
                //clear selected checkboxes
                $('.checkboxGroups').attr('checked', false).change();
                selectedIds = [];
                return false;
            });


            //"Print packaging slips (selected)" button
            $('#exportpackagingslips-selected').click(function (e) {
                e.preventDefault();
                //redirect to required URL
                setLocation('@(Url.Action("PdfPackagingSlips"))?all=false&selectedIds=' + selectedIds.join(","));
                return false;
            });

            
            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes. 
            $(document).on('change', '#shipments-grid input[type=checkbox][id!=mastercheckbox]', function (e) {
                var $check = $(this);
                if ($check.is(":checked") == true) {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked == -1) {
                        //add id to selectedIds.  
                        selectedIds.push($check.val());
                    }
                }
                else {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked > -1) {
                        //remove id from selectedIds.  
                        selectedIds = $.grep(selectedIds, function (item, index) {
                            return item != $check.val();
                        });
                    }
                }
                updateMasterCheckbox();
            });
        });

        function onDataBinding(e) {
            var searchModel = {
                StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
                EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
                TrackingNumber: $('#@Html.FieldIdFor(model => model.TrackingNumber)').val()
            };
            e.data = searchModel;
        }
    
        function onDataBound(e) {

            $('#shipments-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                var currentId = $(this).val();
                var checked = jQuery.inArray(currentId, selectedIds);
                //set checked based on if current checkbox's value is in selectedIds.  
                $(this).attr('checked', checked > -1);
            });

            updateMasterCheckbox();
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#shipments-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#shipments-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
            $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
   </script>
    
}